﻿//创建时间: 2010-11-17
//创建作者: 李 城
//功能说明: 通用的正则处理
//最后修改: 2010-11-17 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Text.RegularExpressions;

namespace TRPOP.Common
{
    /// <summary>
    /// 通用的正则方法
    /// </summary>
    public class CommonRegex
    {
        /// <summary>
        /// 正则表达的选项
        /// </summary>
        private static RegexOptions m_RegexOptions = RegexOptions.Multiline | RegexOptions.IgnoreCase;

        /// <summary>
        /// 匹配 .asp .aspx .htm .html .js .shtml 类型的文件
        /// </summary>
        /// <param name="strFileRoot"></param>
        /// <returns>返回的match：group["root"]是目录  group["filename"]是文件名 group["extname"]是文件后缀名</returns>
        public static Match MatchPubFile(string strFileRoot)
        {
            //(?<root>[\S]*?)[/](?<filename>[\S]*)[.](?<extname>((asp$)|(aspx$)|(htm$)|(html$)))
            string strRegex = @"(?<root>[\S]*[/])" +
                              @"(?<filename>[\S]*)" +
                              @"(?<extname>[.](" +
                                             @"(asp$)|(aspx$)|(htm$)|(html$)|(js$)|(shtml$)|(jsp$)|(php$)|(xml$)" +//在此可修改后缀名
                              @"))";
            Regex oRegex = new Regex(strRegex, m_RegexOptions);
            return oRegex.Match(strFileRoot);
        }
        /// <summary>
        /// 匹配 .asp .aspx .htm .html .js .shtml 类型的文件名
        /// </summary>
        /// <param name="strFileRoot"></param>
        /// <returns></returns>
        public static Match MatchExtFile(string strFileRoot)
        {
            //(?<root>[\S]*?)[/](?<filename>[\S]*)[.](?<extname>((asp$)|(aspx$)|(htm$)|(html$)))
            string strRegex = @"(?<extname>[.](" +
                                             @"(asp$)|(aspx$)|(htm$)|(html$)|(js$)|(shtml$)|(jsp$)|(php$)|(xml$)" +//在此可修改后缀名
                              @"))";
            Regex oRegex = new Regex(strRegex, m_RegexOptions);
            return oRegex.Match(strFileRoot);
        }

        /// <summary>
        /// 匹配 http://test.test.test/test/test.test 等类型的url地址 
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns>返回的match： group["url"]是域名地址,例如: http://www.google.com group["root"]是文件路径，例如: /test/test.test </returns>
        public static Match MatchHttpUrl(string strUrl)
        {
            //(?<url>http://[\S]*?)(?<root>[/][\S]*)
            string strRegex = @"(?<url>http://[\S]*?)" +
                              @"(?<root>[/][\S]*)";
            Regex oRegex = new Regex(strRegex, m_RegexOptions);
            return oRegex.Match(strUrl);
        }
        /// <summary>
        /// 匹配可下载的远程的图片 (gif)|(jpeg)|(jpg)|(bmp)|(png)
        /// </summary>
        /// <param name="strHTML"></param>
        /// <returns></returns>
        public static MatchCollection MatchHttpDownImgFile(string strHTML)
        {
            return new Regex(@"[" + "\"" + @"|'](?<url>http://[\S]*?/[\S]*[.](?<extname>((gif)|(jpeg)|(jpg)|(bmp)|(png))*))[" + "\"" + @"|']", m_RegexOptions).Matches(strHTML);
        }

        /// <summary>
        ///  处理ckfinder上传文件的url问题
        /// </summary>
        /// <param name="strHttpURL">http://dev.cms.yorkbbs.ca/resource/ads</param>
        /// <param name="baseRootFile">/resource/ads</param>
        /// <param name="strHTMLsource"></param>
        public static void UrlAddForCkfinder(string strHttpURL, string baseRootFile, ref string strHTMLsource)
        {
            if (strHttpURL.IsEmpty() == false && baseRootFile.IsEmpty() == false)
            {
                //strHttpURL = strHttpURL.Trim().EndsWith("/") ? strHttpURL.Trim().Remove(strHttpURL.Trim().Length - 1) : strHttpURL.Trim();
                //baseRootFile = baseRootFile.Trim().StartsWith("/") ? baseRootFile.Trim() : (baseRootFile.Trim().Substring(1));
                string strCanNotReplace = strHttpURL;
                strHTMLsource = strHTMLsource.Replace(strCanNotReplace, "$CanNotReplace$");
                strHTMLsource = strHTMLsource.Replace(baseRootFile, strHttpURL);
                strHTMLsource = strHTMLsource.Replace("$CanNotReplace$", strCanNotReplace);
            }
        }

        /// <summary>
        /// 获取HttpURL里的文件名，带路径的
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static string GetHttpUrlFile(string strUrl)
        {
            string strRegex = @"(?<url>(http://[\S]*?)*)" +
                              @"(?<root>[/][\S]*)";
            Regex oRegex = new Regex(strRegex, m_RegexOptions);
            Match oMatch = oRegex.Match(strUrl);
            if (oMatch.Success)
            {
                return oMatch.Groups["root"].Value;
            }
            else
                return "";
        }
        /// <summary>
        /// 目录的转换
        /// </summary>
        /// <param name="strRootReg"></param>
        /// <param name="dtTime"></param>
        /// <returns></returns>
        public static string GetConvertRoot(string strRootReg, DateTime dtTime)
        {
            string strRootName = "";
            string strYYYY = dtTime.ToString("yyyy");
            string strMM = dtTime.ToString("MM");
            string strDD = dtTime.ToString("dd");
            switch (strRootReg.ToLower())
            {
                case "y":
                    strRootName = strYYYY;
                    break;
                case "y-m":
                    strRootName = strYYYY + "-" + strMM;
                    break;
                case "y-m-d":
                    strRootName = strYYYY + "-" + strMM + "-" + strDD;
                    break;
                case "y/m/d":
                    strRootName = strYYYY + "/" + strMM + "/" + strDD;
                    break;
                case "y/m":
                    strRootName = strYYYY + "/" + strMM;
                    break;
                case "ymd":
                    strRootName = strYYYY + strMM + strDD;
                    break;
                case "ym":
                    strRootName = strYYYY + strMM;
                    break;
                case "ym/d":
                    strRootName = strYYYY + strMM + "/" + strDD;
                    break;
                case "y/md":
                    strRootName = strYYYY + "/" + strMM + strDD;
                    break;
                default:
                    strRootName = strRootReg;
                    break;
            }
            return strRootName;
        }

        /// <summary>
        /// 删除HTML中冗余的空格/换行 
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string CutHtmlRedundanceSpace(string strHtml)
        {
            //return Regex.Replace(strHtml, @"[>][\n]{1,}[<]", ">" + System.Environment.NewLine + "<").TrimStart().TrimEnd();
            return Regex.Replace(strHtml, @"[>][\n\r]{1,}[\s\n]+[<]", ">" + System.Environment.NewLine + "<");
        }
    }
}
